<template>
  <div class="login">
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/logo.png" />
      </div>
      <div class="form_box">
        <el-form label-width="0px" :model="form" :rules="rules" ref="form">
          <!-- 用户名 -->
          <el-form-item prop="username">
            <el-input
              prefix-icon="el-icon-user-solid"
              v-model="form.username"
            ></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input
              prefix-icon="el-icon-unlock"
              v-model="form.password"
              type="password"
            ></el-input>
          </el-form-item>
          <!-- 按钮区域 -->
          <el-form-item class="btns">
            <el-button type="primary" @click="login">登录</el-button>
            <el-button type="info" @click="reset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        username: "admin",
        password: "123456",
      },
      rules: {
        username: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    reset() {
      // console.log(this.$refs.form);
      this.$refs.form.resetFields();
    },
    login() {
      this.$refs.form.validate((valid) => {
        // console.log(valid);
        if (!valid) return;
        this.axios
          .post("login", this.form)
          .then((res) => {
            // console.log(res.data);
            if (res.data.meta.status !== 200) return this.$message.error('登录失败')
            this.$message.success("登录成功")
            window.sessionStorage.setItem("token",res.data.data.token)
            this.$router.push("/home")
          });
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.login {
  height: 100%;
  background-color: #2b4b6b;
  background: url("../assets/bg.jpg") no-repeat center/cover;

  .login_box {
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    margin: auto;
    width: 450px;
    height: 300px;
    background-color: #fff;

    .avatar_box {
      position: absolute;
      left: 0;
      right: 0;
      top: -23%;
      margin: auto;
      padding: 10px;
      width: 130px;
      height: 130px;
      border: 1px solid #eee;
      border-radius: 50%;
      box-shadow: 0 0 10px #ddd;
      background-color: #fff;
      img {
        width: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }

    .form_box {
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 0 20px;
      .btns {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>